﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using AntUnion.FrameWork.Model.Models;
using Util.Extension;
using Microsoft.AspNetCore.Hosting;
using Newtonsoft.Json;
using AntUnion.FrameWork.Web.Extend;

namespace AntUnion.FrameWork.Web.Areas.Console.Controllers
{
    public class SystemConfigController : BaseController
    {
        private readonly mtContext _context;
        private readonly IWebHostEnvironment hostingEnv;
        public SystemConfigController(mtContext context, IWebHostEnvironment env) : base(context, env)
        {
            _context = context;
            hostingEnv = env;
        }
        #region 用户页面
        public IActionResult Index(int? pid)
        {
            ViewBag.pid = pid;
            return View();
        }

        /// <summary>
        /// 获取用户页面列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetListJson(int page, int limit, string menuname, bool? status, int pid = -1)
        {
            ListResult res = new ListResult();
            try
            {
                var data = from n in _context.TbSystemConfigs.AsNoTracking()
                           select new
                           {
                               id = n.Id,
                               menuname = n.MenuName,
                               value = n.Value,
                               info = n.Info,
                               status = n.Status,
                               configtab = n.ConfigTab
                           };
                if (!string.IsNullOrEmpty(menuname))
                    data = data.Where(x => x.menuname.Contains(menuname.Trim()));
                if (status != null)
                    data = data.Where(x => x.status == status);
                if (pid != -1)
                    data = data.Where(x => x.configtab == pid);

                res.Data = await data.OrderByDescending(k => k.id).Skip((page - 1) * limit).Take(limit).ToListAsync();

                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 编辑信息
        public async Task<IActionResult> Edit(int? id)
        {
            if (id == null)
            {
                return NotFound();
            }
            TbSystemConfig sg = await _context.TbSystemConfigs.FindAsync(id);
            if (sg == null)
            {
                return NotFound();
            }
            else
            {
                return View(sg);
            }

        }

        /// <summary>
        /// 编辑信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> Edit(TbSystemConfig item)
        {
            Result res = new Result();
            try
            {
                var sg = _context.TbSystemConfigs.Where(x => x.Id == item.Id).FirstOrDefault();
                if (sg != null)
                {
                    sg.MenuName = item.MenuName;
                    sg.Value = item.Value;
                    sg.Info = item.Info;
                    sg.Status = item.Status;
                    res.Code = 0;
                    res.Data = await _context.SaveChangesAsync();
                    RemoveCache();
                }
                else
                {
                    res.Code = 1;
                    res.Data = false;
                }

                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }
        /// <summary>
        /// 更新状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> UpdateState(int id, int intState)
        {
            Result res = new Result();
            try
            {
                TbSystemConfig sg = await _context.TbSystemConfigs.Where(x => x.Id == id).FirstOrDefaultAsync();
                bool state = intState == 1;
                sg.Status = state;
                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                RemoveCache();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region 设置页面
        /// <summary>
        /// 父级configtab的id
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> SetPage(int pid)
        {
            ViewBag.data = await _context.TbSystemConfigs.Where(x => x.ConfigTab == pid && x.Status == true).OrderBy(x => x.Sort).ToListAsync();
            return View();
        }
        [HttpPost]
        public async Task<IActionResult> SetPage(List<SetPageModels> item)
        {
            Result res = new Result();
            using (var bt = _context.Database.BeginTransaction())
            {
                try
                {
                    TbSystemConfig data;
                    foreach (var ii in item)
                    {
                        data = _context.TbSystemConfigs.Where(x => x.Id == ii.id).FirstOrDefault();
                        if (ii.type == "number")
                        {
                            decimal value = ii.value.ToDecimal();
                            if (value < 0)
                            {
                                throw new Exception(data.Info + "的值不能小于0");
                            }
                        }
                        else
                        {
                            data.Value = ii.value;
                        }
                        data.Value = ii.value == null ? "" : ii.value;
                        await _context.SaveChangesAsync();
                    }
                    bt.Commit();
                    RemoveCache();
                    res.Code = 0;
                    return new JsonResult(res);
                }
                catch (Exception ex)
                {
                    bt.Rollback();
                    res.Code = 1;
                    res.Data = ex.Message;
                }
            }

            return new JsonResult(res);
        }
        #endregion

        #region 清除缓存
        public void RemoveCache()
        {
            DefaultCache.Remove(BaseData.Cache.SystemConfig);
        }
        #endregion

        #region 微信模板列表页面
        public IActionResult WxMsgIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取用户页面列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> WxMsgGetListJson(int page, int limit)
        {
            ListResult res = new ListResult();
            try
            {
                var data = from n in _context.TbSystemConfigs.Where(x => x.Type.Contains("wxmsg")).AsNoTracking()
                           select new
                           {
                               id = n.Id,
                               menuname = n.MenuName,
                               info = n.Info,
                               status = n.Status
                           };

                res.Data = await data.OrderBy(k => k.id).Skip((page - 1) * limit).Take(limit).ToListAsync();

                res.Code = 0;
                res.Count = data.Count();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }
        #endregion

        #region  消息模板设置页面
        /// <summary>
        /// 父级configtab的id
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> WxMessnagePage(int id)
        {
            TbSystemConfig sc = await _context.TbSystemConfigs.FindAsync(id);
            WeChatTemplateModel wmsm;
            if (sc == null)
            {
                return NotFound();
            }
            else
            {
                wmsm = JsonConvert.DeserializeObject<WeChatTemplateModel>(sc.Value);
                wmsm.id = id;
            }
            return View(wmsm);
        }

        public async Task<IActionResult> WxMessnagePage(WeChatTemplateModel item)
        {
            Result res = new Result();
            try
            {
                var data = _context.TbSystemConfigs.Where(x => x.Id == item.id).FirstOrDefault();
                WeChatTemplateModel wx = new WeChatTemplateModel
                {
                    touser = string.IsNullOrEmpty(item.touser) ? "orderid" : item.touser,
                    template_id = item.template_id,
                    page = item.page,
                    data = item.data
                };
                data.Value = JsonConvert.SerializeObject(wx);
                var result = await _context.SaveChangesAsync();
                res.Code = 0;
                res.Data = "";
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }
        #endregion

        #region  提现温馨提示
        /// <summary>
        /// 父级configtab的id
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> CashOutExplain()
        {
            TbSystemConfig sc = await _context.TbSystemConfigs.Where(x => x.MenuName == "cash_out_explain").FirstOrDefaultAsync();
            List<CashOutExplain> dfd = new List<CashOutExplain>();
            if (sc == null)
            {
                return NotFound();
            }
            else
            {
                if (!string.IsNullOrEmpty(sc.Value))
                {
                    ViewBag.dfd = sc.Value;
                }
            }
            ViewBag.id = sc.Id;
            return View();
        }

        /// <summary>
        /// 获取客服列表
        /// </summary>
        /// <param name="pid"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> CustomerService()
        {
            TbSystemConfig sc = await _context.TbSystemConfigs.Where(x => x.MenuName == "routine_phone").FirstOrDefaultAsync();
            List<CashOutExplain> dfd = new List<CashOutExplain>();
            if (sc == null)
            {
                return NotFound();
            }
            else
            {
                if (!string.IsNullOrEmpty(sc.Value))
                {
                    ViewBag.dfd = sc.Value;
                }
            }
            ViewBag.id = sc.Id;
            return View();
        }

        public async Task<IActionResult> CashOutExplain(int id, string model)
        {
            Result res = new Result();
            try
            {
                TbSystemConfig sc = await _context.TbSystemConfigs.FindAsync(id);
                sc.Value = model;
                var result = await _context.SaveChangesAsync();
                res.Code = 0;
                res.Data = result;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
            }
            return new JsonResult(res);
        }
        #endregion

        #region 网站设置
        public IActionResult WebEdit()
        {
            List<TbSystemConfig> systemConfigs = _context.TbSystemConfigs.Where(k => k.Status == true && k.ConfigTab == 1 && k.Language == "Public").ToList();
            foreach (TbSystemConfig systemConfig in systemConfigs)
            {
                switch (systemConfig.MenuName)
                {
                    case "webFrontLogo":
                        ViewBag.webFrontLogo = systemConfig.Value;
                        break;
                    case "webAfterLogo":
                        ViewBag.webAfterLogo = systemConfig.Value;
                        break;
                    case "backgroundImage":
                        ViewBag.backgroundImage = systemConfig.Value;
                        break;
                    case "weburl":
                        ViewBag.weburl = systemConfig.Value;
                        break;
                    case "defaultLanguage":
                        ViewBag.defaultLanguage = systemConfig.Value;
                        break;
                    case "settlementTime":
                        ViewBag.settlementTime = systemConfig.Value;
                        break;
                    case "systemMaintenance":
                        ViewBag.systemMaintenance = systemConfig.Value;
                        break;
                }
            }
            EmailSetting email = _context.EmailSettings.Find(1);
            return View(email);
        }

        /// <summary>
        /// 获取用户页面列表
        /// </summary>
        /// <param name="page"></param>
        /// <param name="limit"></param>
        /// <returns></returns>
        [HttpGet]
        public async Task<JsonResult> GetSystemConfigsByLanguage(string language = "zh-CN")
        {
            ListResult res = new ListResult();
            try
            {
                List<TbSystemConfig> systemConfigsByLanguage = await _context.TbSystemConfigs.Where(k => k.Status == true && k.ConfigTab == 1 && k.Language == language).ToListAsync();
                res.Data = systemConfigsByLanguage;
                res.Code = 0;
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Msg = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 基本信息
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> WebEdit(string language, string webFrontName, string webAfterName, string webFrontTitle, string webAfterTitle, string onlineService, string systemMaintenance, string maintenanceExplain)
        {
            Result res = new Result();
            try
            {
                TbSystemConfig webAfterNameConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "webAfterName" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (webAfterNameConfig != null)
                {
                    webAfterNameConfig.Value = webAfterName;
                }
                else
                {
                    webAfterNameConfig = new TbSystemConfig
                    {
                        MenuName = "webAfterName",
                        Value = webAfterName,
                        Info = "网站后台名称",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "input",
                        Sort = 2,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(webAfterNameConfig);
                }

                TbSystemConfig webFrontNameConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "webFrontName" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (webFrontNameConfig != null)
                {
                    webFrontNameConfig.Value = webFrontName;
                }
                else
                {
                    webFrontNameConfig = new TbSystemConfig
                    {
                        MenuName = "webFrontName",
                        Value = webFrontName,
                        Info = "网站前端名称",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "input",
                        Sort = 1,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(webFrontNameConfig);
                }

                TbSystemConfig webFrontTitleConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "webFrontTitle" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (webFrontTitleConfig != null)
                {
                    webFrontTitleConfig.Value = webFrontTitle;
                }
                else
                {
                    webFrontTitleConfig = new TbSystemConfig
                    {
                        MenuName = "webFrontTitle",
                        Value = webFrontTitle,
                        Info = "网站前端Title",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "input",
                        Sort = 3,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(webFrontTitleConfig);
                }

                TbSystemConfig webAfterTitleConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "webAfterTitle" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (webAfterTitleConfig != null)
                {
                    webAfterTitleConfig.Value = webAfterTitle;
                }
                else
                {
                    webAfterTitleConfig = new TbSystemConfig
                    {
                        MenuName = "webAfterTitle",
                        Value = webAfterTitle,
                        Info = "网站后台Title",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "input",
                        Sort = 4,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(webAfterTitleConfig);
                }

                TbSystemConfig onlineServiceConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "onlineService" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (onlineServiceConfig != null)
                {
                    onlineServiceConfig.Value = onlineService;
                }
                else
                {
                    onlineServiceConfig = new TbSystemConfig
                    {
                        MenuName = "onlineService",
                        Value = onlineService,
                        Info = "在线客服代码",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 9,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(onlineServiceConfig);
                }

                TbSystemConfig systemMaintenanceConfig = await _context.TbSystemConfigs.Where(k => k.Language == "Public" && k.MenuName == "systemMaintenance" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (systemMaintenanceConfig != null)
                {
                    systemMaintenanceConfig.Value = systemMaintenance;
                }
                else
                {
                    onlineServiceConfig = new TbSystemConfig
                    {
                        MenuName = "systemMaintenance",
                        Value = systemMaintenance,
                        Info = "维护状态",
                        Status = true,
                        ConfigTab = 1,
                        Type = "checkbox",
                        InputType = "input",
                        Sort = 10,
                        Language = "Public"
                    };
                    _context.TbSystemConfigs.Add(onlineServiceConfig);
                }

                TbSystemConfig maintenanceExplainConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "maintenanceExplain" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (maintenanceExplainConfig != null)
                {
                    maintenanceExplainConfig.Value = maintenanceExplain;
                }
                else
                {
                    onlineServiceConfig = new TbSystemConfig
                    {
                        MenuName = "maintenanceExplain",
                        Value = maintenanceExplain,
                        Info = "维护说明",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 11,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(onlineServiceConfig);
                }

                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 共用设置
        /// </summary>
        /// <param name="defaultLanguage"></param>
        /// <param name="webFrontLogo"></param>
        /// <param name="webAfterLogo"></param>
        /// <param name="backgroundImage"></param>
        /// <param name="weburl"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> WebLogoSettings(string defaultLanguage, string webFrontLogo, string webAfterLogo, string backgroundImage, string weburl, string settlementTime)
        {
            Result res = new Result();
            try
            {
                List<TbSystemConfig> systemConfigs = _context.TbSystemConfigs.Where(k => k.Language == "Public" && k.Status == true && k.ConfigTab == 1).ToList();
                foreach (TbSystemConfig systemConfig in systemConfigs)
                {
                    switch (systemConfig.MenuName)
                    {
                        case "defaultLanguage":
                            systemConfig.Value = defaultLanguage;
                            break;
                        case "backgroundImage":
                            systemConfig.Value = backgroundImage;
                            break;
                        case "webFrontLogo":
                            systemConfig.Value = webFrontLogo;
                            break;
                        case "webAfterLogo":
                            systemConfig.Value = webAfterLogo;
                            break;
                        case "weburl":
                            systemConfig.Value = weburl;
                            break;
                        case "settlementTime":
                            systemConfig.Value = settlementTime;
                            break;
                    }
                }
                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        /// <summary>
        /// 温馨提示
        /// </summary>
        /// <param name="item"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<JsonResult> WarmPromptEdit(string language, string registrationWarmPrompt,
            string userAgreement, string privacyClause, string loginWarmPrompt,
            string passwordWarmPrompt, string personalCenterWarmPrompt, string financeWarmPrompt,
            string withdrawalWarmPrompt, string rechargeWarmPrompt)
        {
            Result res = new Result();
            try
            {
                TbSystemConfig registrationWarmPromptConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "registrationWarmPrompt" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (registrationWarmPromptConfig != null)
                {
                    registrationWarmPromptConfig.Value = registrationWarmPrompt;
                }
                else
                {
                    registrationWarmPromptConfig = new TbSystemConfig
                    {
                        MenuName = "registrationWarmPrompt",
                        Value = registrationWarmPrompt,
                        Info = "注册提示",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 1,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(registrationWarmPromptConfig);
                }

                TbSystemConfig userAgreementConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "userAgreement" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (userAgreementConfig != null)
                {
                    userAgreementConfig.Value = userAgreement;
                }
                else
                {
                    userAgreementConfig = new TbSystemConfig
                    {
                        MenuName = "userAgreement",
                        Value = userAgreement,
                        Info = "用户协议",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 2,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(userAgreementConfig);
                }

                TbSystemConfig privacyClauseConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "privacyClause" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (privacyClauseConfig != null)
                {
                    privacyClauseConfig.Value = privacyClause;
                }
                else
                {
                    privacyClauseConfig = new TbSystemConfig
                    {
                        MenuName = "privacyClause",
                        Value = privacyClause,
                        Info = "隐私条款",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 2,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(privacyClauseConfig);
                }

                TbSystemConfig loginWarmPromptConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "loginWarmPrompt" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (loginWarmPromptConfig != null)
                {
                    loginWarmPromptConfig.Value = loginWarmPrompt;
                }
                else
                {
                    loginWarmPromptConfig = new TbSystemConfig
                    {
                        MenuName = "loginWarmPrompt",
                        Value = loginWarmPrompt,
                        Info = "登录提示",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 2,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(loginWarmPromptConfig);
                }

                TbSystemConfig passwordWarmPromptConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "passwordWarmPrompt" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (passwordWarmPromptConfig != null)
                {
                    passwordWarmPromptConfig.Value = passwordWarmPrompt;
                }
                else
                {
                    passwordWarmPromptConfig = new TbSystemConfig
                    {
                        MenuName = "passwordWarmPrompt",
                        Value = passwordWarmPrompt,
                        Info = "忘记密码",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 2,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(passwordWarmPromptConfig);
                }

                TbSystemConfig webFrontNameConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "personalCenterWarmPrompt" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (webFrontNameConfig != null)
                {
                    webFrontNameConfig.Value = personalCenterWarmPrompt;
                }
                else
                {
                    webFrontNameConfig = new TbSystemConfig
                    {
                        MenuName = "personalCenterWarmPrompt",
                        Value = personalCenterWarmPrompt,
                        Info = "个人信息",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 2,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(webFrontNameConfig);
                }

                TbSystemConfig webFrontTitleConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "financeWarmPrompt" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (webFrontTitleConfig != null)
                {
                    webFrontTitleConfig.Value = financeWarmPrompt;
                }
                else
                {
                    webFrontTitleConfig = new TbSystemConfig
                    {
                        MenuName = "financeWarmPrompt",
                        Value = financeWarmPrompt,
                        Info = "财务信息",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 3,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(webFrontTitleConfig);
                }

                TbSystemConfig withdrawalWarmPromptConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "withdrawalWarmPrompt" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (withdrawalWarmPromptConfig != null)
                {
                    withdrawalWarmPromptConfig.Value = withdrawalWarmPrompt;
                }
                else
                {
                    withdrawalWarmPromptConfig = new TbSystemConfig
                    {
                        MenuName = "withdrawalWarmPrompt",
                        Value = withdrawalWarmPrompt,
                        Info = "提现提示",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 3,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(withdrawalWarmPromptConfig);
                }

                TbSystemConfig rechargeWarmPromptConfig = await _context.TbSystemConfigs.Where(k => k.Language == language && k.MenuName == "rechargeWarmPrompt" && k.Status == true && k.ConfigTab == 1).FirstOrDefaultAsync();
                if (rechargeWarmPromptConfig != null)
                {
                    rechargeWarmPromptConfig.Value = rechargeWarmPrompt;
                }
                else
                {
                    rechargeWarmPromptConfig = new TbSystemConfig
                    {
                        MenuName = "rechargeWarmPrompt",
                        Value = rechargeWarmPrompt,
                        Info = "充值提示",
                        Status = true,
                        ConfigTab = 1,
                        Type = "text",
                        InputType = "textarea",
                        Sort = 3,
                        Language = language
                    };
                    _context.TbSystemConfigs.Add(rechargeWarmPromptConfig);
                }

                res.Code = 0;
                res.Data = await _context.SaveChangesAsync();
                return new JsonResult(res);
            }
            catch (Exception ex)
            {
                res.Code = 1;
                res.Data = ex.Message;
                return new JsonResult(res);
            }
        }

        #endregion
    }

    public class SetPageData
    {
        public List<SetPageModels> data { get; set; }
    }

    public class SetPageModels
    {
        public int id { get; set; }
        public string type { get; set; }
        public string value { get; set; }
    }

    public class WeChatTemplateModel
    {
        /// <summary>
        /// 模板编号
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 模板id
        /// </summary>
        public string template_id { get; set; }
        /// <summary>
        /// 跳转链接
        /// </summary>
        public string page { get; set; }
        /// <summary>
        /// 发送的用户
        /// </summary>
        public string touser { get; set; }
        /// <summary>
        /// 数据格式
        /// </summary>
        public List<WeChatTemplateData> data { get; set; }
    }

    public class CashOutExplain
    {
        /// <summary>
        /// 序号
        /// </summary>
        public int id { get; set; }
        /// <summary>
        /// 说明
        /// </summary>
        public string explain { get; set; }
    }


    public class WeChatTemplateData
    {
        public string name { get; set; }
        public string value { get; set; }
    }

    public class DeliveryFeeData
    {
        public decimal min { get; set; }
        public decimal max { get; set; }
        public decimal value { get; set; }
    }
}
